/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 8/30/11
 * Time: 1:35 AM
 * To change this template use File | Settings | File Templates.
 */

import javax.print.attribute.Size2DSyntax;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.IOException;
import java.util.Arrays;

public class TaskC extends Thread {

    public TaskC() {
        this.input = new BufferedReader(new InputStreamReader(System.in));
        this.output = new PrintWriter(System.out);
        this.setPriority(Thread.MAX_PRIORITY);
    }

    private static final boolean isLucky(int toCheck) {
        boolean ok = true;
        while (toCheck != 0) {
            int remainder = toCheck % 10;
            toCheck /= 10;
            ok &= remainder == 4 || remainder == 7;
        }
        return ok;
    }

    private void dfs(int i) {
        if (used[i]) {
            return;
        } else {
            ++M;
            used[i] = true;
            for (int current : g[i]) {
                dfs(current);
            }
        }
    }

    private void solve() throws Throwable {
        int n = nextInt();
        long N = n;
        long result = N * (N - 1) * (N - 2);
        g = new ArrayList [n];
        used = new boolean[n];
        for (int i = 0; i < n; ++i) {
            g[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i + 1 < n; ++i) {
            int a = nextInt() - 1, b = nextInt() - 1, what = nextInt();
            if (!isLucky(what)) {
                g[a].add(b);
                g[b].add(a);
            }
        }
        for (int i = 0; i < n; ++i) {
            if (!used[i]) {
                M = 0;
                dfs(i);
                result -= M * (M - 1) * (2 * (N - 1) - M);
            }
        }
        output.println(result);

    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }


    public static void main(String[] args) {
        new TaskC().start();
    }

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    ArrayList<Integer> []g;
    boolean []used;
    long M;
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}